Skip to content

fix: 修复代码块吞正文(闭合围栏被误改为 ```text) - #3

Open
turinglambdaai wants to merge 1 commit into
masterfrom
fix/code-fence-swallow
Open

fix: 修复代码块吞正文(闭合围栏被误改为 ```text)#3
turinglambdaai wants to merge 1 commit into
masterfrom
fix/code-fence-swallow

Conversation

@turinglambdaai

Copy link
Copy Markdown
Owner

问题

从「在这种疑虑的情况下,你就不得不做最坏的打算,你就得把代码写成:」开始,下面所有正文被包裹在代码块里无法正常渲染。

根因

crawler.pyformat_content() 围栏正则 ^\``\w*$无法识别含特殊字符的语言标记(如 c# `、` c++ ,因为 #/+不属于\w`)。

因果链:

  1. 源站数据正确(```c# 开 / ``` 闭)
  2. ```c# 不匹配正则 → 开启围栏漏识别 → 状态机 in_code_block 不翻转
  3. 遇到合法闭合围栏 ``` → 状态机误判为「开启」→ 满足 not in_code_block and stripped == "\``"→ 改写成 ```text `
  4. CommonMark 规定闭合围栏不能带 info string → ```text 成了新代码块的起始标记 → 后续正文全部被吞

影响范围

3 篇文章、共 16 处坏围栏(用项目 markdown-it 实际渲染检测):

  • Kotlin 和 Checked Exception.md(1 处)← 用户报告
  • C# 的 IDisposable 接口.md(5 处)
  • 对 Rust 语言的分析.md(10 处)

修复

1. scripts/crawler.py — 修围栏正则与状态机

  • 正则改为 ^(\s{0,3})(\{3,})([^\`])$:可识别任意语言标记(c#c++f#…),[^\`] 同时排除行内代码( code `)
  • 状态机改为显式开启/闭合分支:开启→若裸则加 text;闭合→保持不动

2. scripts/fix_code_fences.py — 一次性数据修复(新增)

用修复后的状态机扫描全部文章,将错误的 ```text 闭合围栏还原为裸 ```。幂等(已验证再跑 0 改动)。

3. 修复 3 篇文章的 16 处坏数据

验证

用项目实际 markdown-it 重新渲染全部 173 篇文章0 处代码块吞正文

检查 173 篇文章
✅ 全部渲染正常

备注

本 PR 只修代码块渲染 bug。AdSense「收益损失风险」警告经排查不在代码层(ads.txt 内容正确、HTTP 200、AdSense 爬虫 UA 也返回 200),属账号侧问题。

crawler.py 的 format_content() 围栏正则 ^```\w*$ 无法识别含特殊
字符的语言标记(如 ```c#、```c++,因 # 不属于 \w),导致开启围栏
漏识别、状态机不翻转。随后遇到合法的闭合围栏 ``` 时被误判为开启
围栏,改写成 ```text —— 而 CommonMark 规定闭合围栏不能带 info
string,于是 ```text 成了新代码块的起始标记,后续所有正文被吞进
未闭合的代码块。

影响 3 篇文章共 16 处坏围栏:
- Kotlin 和 Checked Exception.md(1 处)
- C# 的 IDisposable 接口.md(5 处)
- 对 Rust 语言的分析.md(10 处)

修复:
1. crawler.py: 围栏正则改为 ^(\s{0,3})(`{3,})([^`]*)$,可识别任意
   语言标记;状态机区分开启/闭合分支,闭合围栏不再被改写。
2. 新增 scripts/fix_code_fences.py: 用修复后的状态机扫描全部文章,
   将错误的 ```text 闭合围栏还原为裸 ```(幂等,已跑 0 改动验证)。
3. 修复 3 篇文章的 16 处坏数据。

验证:用项目 markdown-it 重新渲染全部 173 篇文章,0 处代码块吞正文。
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
yinwangdl Ready Ready Preview Aug 9, 2026 1:40pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant